package com.winspread.app.drawable

import android.graphics.*
import android.graphics.drawable.Drawable
import com.winspread.app.tools.px
import kotlin.math.sin

/**
 *Author: Amin
 *Data: 2022/4/14 19:35
 *
 *Description:
 *
 */
class BallDrawable : Drawable() {

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.FILL
        color = Color.parseColor("#d2691e")
    }

    private val linePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
        strokeWidth = 1f.px
        color = Color.BLACK
    }

    override fun draw(canvas: Canvas) {
        val radius = bounds.width().toFloat() / 2
        //画球的圆
        canvas.drawCircle(
            bounds.width().toFloat() / 2,
            bounds.width().toFloat() / 2,
            radius,
            paint
        )
        //球的竖线线
        canvas.drawLine(
            bounds.width().toFloat() / 2,
            0f,
            bounds.width().toFloat() / 2,
            bounds.height().toFloat(),
            linePaint
        )
        //球的横线
        canvas.drawLine(
            0f,
            bounds.height().toFloat() / 2,
            bounds.width().toFloat(),
            bounds.height().toFloat() / 2,
            linePaint
        )

        val path = Path()
        val sinValue = sin(Math.toRadians(45.0)).toFloat()
        //左曲线
        path.moveTo(
            radius - sinValue * radius,
            radius - sinValue * radius
        )

        path.cubicTo(
            radius - sinValue * radius,
            radius - sinValue * radius,
            radius,
            radius,
            radius - sinValue * radius,
            radius + sinValue * radius
        )
        //右曲线
        path.moveTo(
            radius+sinValue*radius,
            radius-sinValue*radius
        )

        path.cubicTo(
            radius+sinValue*radius,
            radius-sinValue*radius,
            radius,radius,
            radius+sinValue*radius,
            radius+sinValue*radius
        )
        canvas.drawPath(path,linePaint)


    }

    override fun setAlpha(alpha: Int) {
        paint.alpha = alpha
    }

    override fun setColorFilter(colorFilter: ColorFilter?) {
        paint.colorFilter = colorFilter
    }

    override fun getOpacity(): Int {
        return when(paint.alpha){
            0xff->PixelFormat.OPAQUE
            0x00->PixelFormat.TRANSPARENT
            else-> PixelFormat.TRANSLUCENT
        }
    }
}